# Replication materials ---------------------------------------------------
# Paper:    Campaigns and the Selection of Policy-seeking Representatives.
#           Legislative Studies Quarterly
# Authors: Shaun Bowler, Gail McElroy, Stefan Müller


#--------------------------------------------------------------------------
# Script to reproduce all analyses from the paper and Supplementary Material
#--------------------------------------------------------------------------

# Load packages -----------------------------------------------------------

library(cowplot)
library(texreg)
library(car)
library(MASS)
library(Hmisc)
library(dotwhisker)
library(gridExtra)
library(effects)
library(lme4)
library(haven)
library(tidyverse)

# create ggplot2 theme for paper
theme_baser <- function (){
  theme_minimal()  %+replace%
    theme(panel.grid.minor.x = element_blank(),
          panel.grid.minor.y = element_blank(),
          panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_blank(),
          panel.border = element_rect(fill=NA,color="black", size=0.5,
                                      linetype="solid"),
          legend.title = element_text(size = 15),
          title = element_text(size = 15, vjust = 1.5, hjust = 0),
          legend.position = "bottom",
          axis.ticks = element_line(size = 0.3),
          axis.ticks.length = unit(0.3,"cm"),
          legend.text=element_text(size = 13),
          strip.text = element_text(size = 15, hjust = 0.5,
                                    margin=margin(b=5, r = 5, l = 5, t = 5)),
          axis.text = element_text(colour="black", size = 13),
          axis.title = element_text(size = 13, hjust = 0.5))
}

theme_set(theme_baser())

# THE DATA FOR THE COMPARATIVE CANDIDATE STUDY (MODULES 1 AND 2)
# MUST BE REQUESTED FROM FORS. SEE: http://www.comparativecandidates.org/

ccs1 <- read_sav("/Users/smueller/Downloads/620_CCS_Data_Wave1_v4.0.sav")
ccs2 <- read_sav("/Users/smueller/Downloads/886_CCS_Data_Wave2_v1.0.sav")

recode_study_wave2 <- "'1'='Germany 2013';
'2'='Switzerland 2015'; '3'='Canada 2015';
'4'='Greece 2015'; '5'='Australia 2013';
'6'='Sweden 2014'; '7'='Norway 2013';
'8'='Hungary 2014'; '9'='Montenegro 2012';
'10'='Montenegro 2016';'11'='Albania 2013';
'12'='Iceland 2013'; '13'='Romania 2016'"


ccs2 <- ccs2 %>% 
  mutate(study_id = car::recode(T1, recode_study_wave2)) %>% 
  mutate(country_id = substr(study_id, 1, nchar(study_id)-5)) %>% 
  mutate(study_id = as.factor(study_id)) %>% 
  mutate(elected_dummy = as.factor(T11)) %>% 
  mutate(gender = if_else(E1 == '1', 'Male', 'Female')) %>% 
  mutate(gender = as.factor(gender)) %>% 
  mutate(incumbent = as.character(A4b2)) %>% 
  mutate(incumbent = car::recode(incumbent, "'1'='TRUE';'2'='FALSE'")) %>% 
  mutate(incumbent = as.factor(incumbent)) %>% 
  mutate(party_id = as.factor(A1)) %>% 
  mutate(party_country_id = as.factor(paste(T1, A1, sep = "_")))


vars <- c("B4a", "B4a", "B4c", "B4d", "B4e", 
          "B4f", "B4g", "B4h", "B4i", "B4j",
          "B4k", "B4l", "B4m", "B4n", "B4o",
          "B4p", "B4q", "B4r", "B4s")

ccs2_ordinal <- ccs2 %>% 
  mutate_at(c(vars), funs(car::recode(., 
                                      "0=0; 1=1;
                                        2=2; 3=3; 4=4; 5=5;-9=NA;-1=NA"))) %>% 
  rowwise() %>%
  mutate(use_campaign = sum(B4a, B4b, B4c, B4d, B4e, B4f, B4g, B4h, B4i +
                              B4j, B4k, B4l, B4m, B4n, B4o, B4p, B4q, B4r, B4s, na.rm = TRUE)) %>% 
  dplyr::select(study_id, elected_dummy, gender,
                use_campaign, incumbent, country_id, party_id, party_country_id) %>% 
  mutate(wave = "Wave II")

ccs2_dummies <- ccs2 %>% 
  mutate_at(c(vars), funs(car::recode(., 
                                      "0=0; 1=1;
                                           2=1; 3=1; 4=1; 5=1"))) %>% 
  rowwise() %>% 
  mutate(use_campaign = sum(B4a, B4b, B4c, B4d, B4e, B4f, B4g, B4h, B4i +
                              B4j, B4k, B4l, B4m, B4n, B4o, B4p, B4q, B4r, B4s, na.rm = TRUE)) %>% 
  ungroup() %>% 
  dplyr::select(study_id, elected_dummy, gender,
                use_campaign, incumbent) %>% 
  mutate(wave = "Wave II")


recode_country_wave1 <- "'1'= 'Australia';
'2'= 'Switzerland';
'3'= 'Germany';
'4'= 'Ireland';
'5'= 'Greece';
'6'= 'Finland';
'7'= 'Belgium';
'8'= 'Netherlands';
'9'= 'Canada';
'10'= 'Portugal';
'11'= 'Estonia';
'12'= 'Iceland';
'13'= 'Hungary';
'14'= 'Austria';
'15'= 'Sweden';
'16'= 'Denmark';
'17'='Romania';
'18'='Czech Republic';
'19'='Norway';
'20'='New Zealand';
'21'='Italy';
'22'='Malta';
'23'='United Kingdom';
'24'='Luxembourg'"


vars_ccs1_b5 <- c("b5a1", "b5a2", "b5a3", "b5a4", "b5a5", 
                  "b5a6", "b5a7", "b5a8", "b5a9", "b5a10",
                  "b5b1", "b5b2", "b5b3", "b5b4", "b5b5",
                  "b5b6", "b5b7")

vars_ccs1_b6 <- c("b6a1", "b6a2", "b6a3",
                  "b6a4", "b6a5", "b6a6", "b6a7")



ccs1 <- ccs1 %>% 
  mutate(country_id = car::recode(t1, recode_country_wave1)) %>%
  mutate(study_id = paste(country_id, t3, sep = " ")) %>% 
  mutate(study_id = as.factor(study_id)) %>% 
  mutate(elected_dummy = as.factor(t8)) %>% 
  mutate(gender = if_else(e1 == '1', 'Male', 'Female')) %>% 
  mutate(gender = as.factor(gender)) %>% 
  mutate(incumbent = as.character(a4b2)) %>% 
  mutate(incumbent = car::recode(incumbent, "'1'='TRUE';'2'='FALSE'")) %>% 
  mutate(incumbent = as.factor(incumbent)) %>% 
  mutate(party_country_id = as.factor(paste(country_id, a1, sep = "_"))) %>% 
  mutate(party_id= as.factor(a1)) 


ccs1_ordinal <- ccs1 %>% 
  mutate_at(c(vars_ccs1_b5), funs(car::recode(., 
                                              "1=0; 2=1;
                                        3=2; 4=3; 5=4; 6=5;-9=NA;-1=NA"))) %>% 
  mutate_at(c(vars_ccs1_b6), funs(car::recode(., 
                                              "1=1; 2=0;-9=NA;-1=NA"))) %>% 
  rowwise() %>% 
  mutate(use_campaign = sum(b5a1, b5a2, b5a3, b5a4, b5a5, 
                            b5a6, b5a7, b5a8, b5a9, b5a10 + 
                              b5b1, b5b2, b5b3, b5b4, b5b5 + 
                              b5b6, b5b7, b6a1, b6a2, b6a3 + 
                              b6a4, b6a5, b6a6, b6a7, na.rm = TRUE)) %>% 
  ungroup() %>% 
  dplyr::select(study_id, elected_dummy, gender,
                use_campaign, incumbent, country_id, party_id, party_country_id) %>% 
  mutate(wave = "Wave I")

ccs1_dummies <- ccs1 %>% 
  mutate_at(c(vars_ccs1_b5), funs(car::recode(., 
                                              "1=0; 2=1;
                                                3=1; 4=1; 5=1; 6=1;-9=NA;-1=NA"))) %>% 
  mutate_at(c(vars_ccs1_b6), funs(car::recode(., 
                                              "1=1; 2=0;-9=NA;-1=NA"))) %>%   rowwise() %>% 
  mutate(use_campaign = sum(b5a1, b5a2, b5a3, b5a4, b5a5, 
                            b5a6, b5a7, b5a8, b5a9, b5a10 + 
                              b5b1, b5b2, b5b3, b5b4, b5b5 + 
                              b5b6, b5b7, b6a1, b6a2, b6a3 + 
                              b6a4, b6a5, b6a6, b6a7, na.rm = TRUE)) %>% 
  ungroup() %>% 
  dplyr::select(study_id, elected_dummy, incumbent, gender,
                use_campaign, party_country_id, party_id) %>% 
  mutate(wave = "Wave I")

ccs_combined_dummies <- bind_rows(ccs1_dummies, ccs2_dummies) %>% 
  filter(use_campaign > 0 & !is.na(incumbent))

# Dail 30 (2007-2011)
Dail30 <- read.csv("Dail30_final.csv", sep = ",", header = TRUE, fileEncoding = "utf-8") %>% 
  transmute(incumbent = car::recode(incumbent, "1='TRUE';0='FALSE'"),
            incumbent = as.factor(incumbent),
            study_id = "Ireland 2007",
            use_campaign = use_campaign,
            use_ord_campaign = use_ord_total,
            party_id = partyCCS,
            gender = Gender,
            elected_dummy = car::recode(won, "1=1;else=0")) %>% 
  mutate(elected_dummy = as.factor(elected_dummy)) %>% 
  filter(use_campaign > 0 & !is.na(use_campaign))


# Dail 31 (2011-2016)
Dail31 <- read.csv("Dail31_final.csv", 
                   sep = ",", header = TRUE, fileEncoding = "utf-8") %>% 
  transmute(incumbent = car::recode(incumbent, "1='TRUE';0='FALSE'"),
            incumbent = as.factor(incumbent),
            study_id = "Ireland 2011",
            party_id = as.factor(partyCCS),
            use_campaign = use_campaign,
            use_ord_campaign = use_ord_total,
            gender = gender,
            elected_dummy = car::recode(wonseatCCS, "2=1;1=0")) %>% 
  mutate(elected_dummy = as.factor(elected_dummy)) %>% filter(use_campaign > 0)


ccs_combined_dummies_with_ire <- bind_rows(Dail30, 
                                           Dail31, ccs_combined_dummies) %>% 
  mutate(gender = as.factor(gender)) 


ccs_combined_dummies_with_ire <- ccs_combined_dummies_with_ire %>% 
  mutate(country_id = as.factor(substr(study_id, 1, nchar(ccs_combined_dummies_with_ire$study_id) -4))) %>% 
  mutate(country_id = str_sub(study_id, 1, str_length(study_id)-5)) %>% 
  mutate(country_id = as.factor(country_id)) %>% 
  mutate(study_id = as.factor(study_id))

ccs_combined_dummies$gender <- factor(ccs_combined_dummies$gender, 
                                      levels = c("Male", "Female"))

ccs_combined_dummies$study_id <- factor(ccs_combined_dummies$study_id)

ccs_combined_dummies$party_id <- as.factor(ccs_combined_dummies$party_id)


dail_30_ord <- Dail30 %>%
  dplyr::select(-use_campaign) %>%
  rename(use_campaign = use_ord_campaign)

dail_31_ord <- Dail31 %>%
  dplyr::select(-use_campaign) %>%
  rename(use_campaign = use_ord_campaign)

ccs_combined_ordinal <- bind_rows(ccs1_ordinal, ccs2_ordinal) %>% 
  filter(use_campaign > 0 & !is.na(incumbent))

ccs_combined_ordinal <- bind_rows(ccs_combined_ordinal,
                                  dail_30_ord,
                                  dail_31_ord)


set.seed(44)

dta <- ccs_combined_dummies_with_ire %>% 
  filter(!is.na(elected_dummy) & !is.na(gender) & !is.na(incumbent) & !is.na(use_campaign)) %>% 
  mutate(country_id = as.character(country_id)) %>% 
  mutate(country_id = as.factor(country_id))


data_combined_model <- ccs_combined_dummies_with_ire %>% 
  dplyr::select(elected_dummy, use_campaign, gender, incumbent, study_id) %>% 
  na.omit() %>% 
  group_by(study_id)

data_combined_model <- ccs_combined_dummies_with_ire %>% 
  dplyr::select(elected_dummy, use_campaign, country_id, gender, incumbent, study_id) %>% 
  na.omit() %>% 
  group_by(study_id)

data_combined_model$gender <- factor(data_combined_model$gender,
                                     levels = c("Male", "Female"))

glmer_dummies <- glmer(elected_dummy ~ use_campaign * incumbent + 
                         gender +  incumbent  + (1 | study_id),
                       family = binomial(link = "logit"),
                       data = data_combined_model)



## Figure 1 ----

get_effect_incumbent <- Effect(c("use_campaign", "incumbent"), 
                               glmer_dummies, xlevels = 40) %>% 
  as.data.frame() 

plot_interact <- ggplot(data = get_effect_incumbent, 
                        aes(x = use_campaign, y = fit,
                            fill = incumbent)) + 
  scale_linetype_discrete(name = NULL, 
                          labels = c("Nonincumbent", "Incumbent")) +
  scale_fill_grey(name = NULL, 
                  labels = c("Nonincumbent", "Incumbent"),
                  start = 0.4, end = 0.7) +
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.8) +
  geom_line(aes(linetype = incumbent), colour = "black") + 
  labs(x = NULL, y = "Predicted probabilitiy of getting elected") +   
  theme(legend.justification = c(0, 0), 
        legend.position = c(0.01, 0.8)) +
  scale_x_continuous(labels = NULL) +
  guides(linetype = guide_legend(reverse = TRUE),
         fill = guide_legend(reverse = TRUE)) +
  theme(axis.ticks.x = element_blank())
plot_interact

plot_hist <- ggplot(data = data_combined_model, 
                    aes(x = use_campaign)) +
  geom_bar() + 
  scale_y_continuous(breaks = c(400, 800)) +
  labs(x = "Campaign activity", y = "Count") +
  theme(panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'))

grid_dummies <- plot_grid(plot_interact, plot_hist, align = "v", nrow = 2, rel_heights = c(4/5, 1/5))
grid_dummies

ggsave(grid_dummies, file = "fig_01.pdf", 
       width = 10, height = 5)
ggsave(grid_dummies, file = "fig_01.png", 
       width = 10, height = 5)


# Load final datasets -----------------------------------------------------

# European Parliament 6 (2004-2009)
ep_6_final <- read_csv("EP6_final.csv") %>% 
  mutate(country_id = "EP")

ep_6_reg <- ep_6_final %>% 
  filter(leg_activity > 1 & leg_activity_month_round < 50 
         & use_campaign > 1) 

# European Parliament 7 (2009-2014)
ep_7_final <- read_csv("EP7_final.csv") %>% 
  mutate(country_id = "EP")

ep_7_reg <- ep_7_final %>% 
  filter(leg_activity > 1 & leg_activity_month_round < 70 & use_campaign > 1)

# Dail 30 (2007-2011)
dail_30_final <- read_csv("Dail30_final.csv")

dail_30_reg <- dail_30_final %>% 
  filter(party != "Ceann Comhairle" & use_campaign > 1)

# Dail 31 (2011-2016)
dail_31_final <- read_csv("Dail31_final.csv")

dail_31_reg <- dail_31_final %>% 
  filter(use_campaign > 1) 

# Bundestag 16 (2005-2009)
bt_16_final <- read_csv("BT16_final.csv")

bt_16_reg <- bt_16_final %>% 
  filter(wonseat == 1 & 
           use_campaign > 1 & leg_activity > 0)


### transform data to long format ----


bt_16_long <- bt_16_reg %>% 
  dplyr::select(starts_with("use_"), leg_activity,
                -c(starts_with("use_ord_"))) %>% 
  gather(var, value, -c(leg_activity)) %>% 
  mutate(cycle_id = "Germany (2005-2009)") %>% 
  dplyr::filter(!var %in% c("use_classic", "use_postmodern", 
                            "use_campaign", "use_material")) %>% 
  mutate(leg_activity = scales::rescale(leg_activity, to = c(0, 1)))


ep_7_long <- ep_7_reg %>% 
  dplyr::select(starts_with("use_"), leg_activity_month_round,
                -c(starts_with("use_ord_"))) %>% 
  gather(var, value, -c(leg_activity_month_round)) %>% 
  mutate(cycle_id = "European Parliament 7 (2009-2014)") %>% 
  dplyr::filter(!var %in% c("use_classic", "use_postmodern", 
                            "use_campaign", "use_material")) %>% 
  dplyr::filter(!var %in% c("use_classic", "use_postmodern", 
                            "use_campaign", "use_material")) %>% 
  mutate(leg_activity = scales::rescale(leg_activity_month_round, to = c(0, 1)))


ep_6_long <- ep_6_reg %>% 
  dplyr::select(starts_with("use_"), leg_activity_month_round,
                -c(starts_with("use_ord_"))) %>% 
  gather(var, value, -c(leg_activity_month_round)) %>% 
  mutate(cycle_id = "European Parliament 6 (2004-2009)") %>% 
  dplyr::filter(!var %in% c("use_classic", "use_postmodern", 
                            "use_campaign", "use_material")) %>% 
  mutate(leg_activity = scales::rescale(leg_activity_month_round, to = c(0, 1)))


dail_30_long <- dail_30_reg %>% 
  mutate(leg_activity = words_total / 10000) %>% 
  dplyr::select(starts_with("use_"), leg_activity,
                -c(starts_with("use_ord_"))) %>% 
  gather(var, value, -c(leg_activity)) %>% 
  mutate(cycle_id = "Ireland (2007-2011)") %>% 
  dplyr::filter(!var %in% c("use_classic", "use_postmodern", 
                            "use_campaign", "use_material")) %>% 
  mutate(leg_activity = scales::rescale(leg_activity, to = c(0, 1)))


dail_31_long <- dail_31_reg %>% 
  mutate(leg_activity = words_total / 10000) %>% 
  dplyr::select(starts_with("use_"), leg_activity, -c(starts_with("use_ord_"))) %>% 
  gather(var, value, -c(leg_activity)) %>% 
  mutate(cycle_id = "Ireland (2011-2016)") %>% 
  dplyr::filter(!var %in% c("use_classic", "use_postmodern", 
                            "use_campaign", "use_material")) %>% 
  mutate(leg_activity = scales::rescale(leg_activity, to = c(0, 1)))


# bind all "long" datasets
data_long <- bind_rows(dail_31_long, dail_30_long,
                       ep_6_long, ep_7_long,
                       bt_16_long)


## Get correlation coefficients for Table A2
cor.test(ep_6_reg$leg_activity_month_round, ep_6_reg$vote_activity)

cor.test(ep_7_reg$leg_activity_month_round, ep_7_reg$vote_activity)

cor.test(dail_31_reg$words_total, dail_31_reg$vote_activity)

cor.test(bt_16_reg$speeches, bt_16_reg$leg_activity)


#####
# Run regression models ----
#####

## Adjust variable names here for joint model
## bind to new dataframe and scale by group

ep_6_combined <- ep_6_reg %>% 
  mutate(leg_activity = leg_activity_month_round,
         female = factor(car::recode(female, "1='Female';0='Male'"))) %>% 
  mutate(cycle_id = "European Parliament 6 (2004-2009)") %>% 
  dplyr::select(cycle_id, incumbent, 
                country_id, leg_activity, female, 
                age, use_campaign, age_squared, 
                open_list, committee_chair, 
                partypos, party_group, country,
                vote_activity) %>% 
  mutate(partypos = as.factor(partypos))


ep_7_combined <- ep_7_reg %>% 
  mutate(leg_activity = leg_activity_month_round,
         female = factor(car::recode(female, "1='Female';0='Male'"))) %>% 
  mutate(cycle_id = "European Parliament 7 (2009-2014)") %>% 
  dplyr::select(cycle_id, incumbent, country_id, 
                leg_activity, female, age,
                use_campaign, age_squared, open_list,
                committee_chair, partypos, vote_activity, 
                vote_activity_month,
                party_group, country) %>% 
  mutate(partypos = as.factor(partypos-1))

dail_30_combined <- dail_30_reg %>% 
  mutate(leg_activity = words_total / 10000,
         female = factor(car::recode(female, "1='Female';0='Male'"))) %>% 
  mutate(cycle_id = "Ireland (2007-2011)") %>% 
  dplyr::select(cycle_id, country_id, 
                incumbent, government, 
                leg_activity, female, age, minister, use_campaign, age_squared)

dail_31_combined <- dail_31_reg %>% 
  mutate(leg_activity = words_total / 10000,
         female = factor(car::recode(female, "1='Female';0='Male'"))) %>% 
  mutate(cycle_id = "Ireland (2011-2016)") %>% 
  dplyr::select(cycle_id, country_id, vote_activity, minister, incumbent, government, leg_activity, female, age, use_campaign, age_squared) 


bt_16_combined <- bt_16_reg %>% 
  mutate(leg_activity = leg_activity,
         female = factor(car::recode(female, "1='Female';0='Male'"))) %>% 
  mutate(cycle_id = "Germany (2005-2009)") %>% 
  dplyr::select(cycle_id,country_id, 
                incumbent, speeches, government, 
                leg_activity, female, age,
                use_campaign, age_squared)


data_combined <- bind_rows(ep_6_combined, ep_7_combined, 
                           dail_30_combined, dail_31_combined,
                           bt_16_combined) %>% 
  mutate(cycle_id = as.factor(cycle_id)) %>% 
  filter(!is.na(leg_activity))

data_combined <- data_combined %>% 
  group_by(cycle_id) %>% 
  mutate(leg_activity_scaled01 = scales::rescale(leg_activity, c(0, 1))) %>% 
  mutate(use_campaign_scaled01 = scales::rescale(use_campaign, c(0, 1)))


data_combined$party_group[data_combined$party_group=="Group of the European People's Party (Christian Democrats) and European Democrats"] <- "EPP"

recode_partygroup_ep <- c("'Alliance of Liberals and Democrats for Europe'='ALDE';
                          'European United Left/Nordic Green Left'='EUL-NGL';
                          'The Greens/European Free Alliance'='Greens-EFA';
                          'Ohter'='Other';
                          'Party of European Socialists'='SD';
                          'Independence/Democracy Group'='IND/DEM';
                          'Union for Europe of the Nations'='UEN'")

data_combined_ep <- data_combined %>%
  ungroup() %>% 
  filter(cycle_id %in% c("European Parliament 6 (2004-2009)",
                         "European Parliament 7 (2009-2014)")) %>% 
  mutate(party_group = as.factor(car::recode(party_group, recode_partygroup_ep))) %>% 
  mutate(open_list = as.factor(open_list)) %>% 
  mutate(country = as.factor(stringr::str_to_lower(country))) %>% 
  mutate(female = as.factor(female)) %>% 
  mutate(partypos = as.factor(partypos)) %>% 
  mutate(incument = as.factor(incumbent))

data_combined_ep$female <- factor(data_combined_ep$female,
                                  levels = c("Male", "Female"))

data_combined_ep_act <- data_combined_ep %>% 
  dplyr::select(cycle_id, use_campaign, leg_activity, vote_activity) %>% 
  gather(type_activity, leg_activity, -c(cycle_id, use_campaign)) %>% 
  mutate(type_activity = car::recode(type_activity, 
                                     "'leg_activity'='Monthly procedural activity';
                                     'vote_activity'='Participation in roll-call votes'"))


data_combined_ire_ger <- data_combined %>% 
  filter(country_id %in% c("Ireland", "Germany")) %>% 
  dplyr::select(cycle_id, speeches, country_id, use_campaign, government, leg_activity, vote_activity) %>% 
  gather(type_activity, leg_activity, 
         -c(cycle_id, country_id, government, use_campaign)) %>% 
  filter(!is.na(leg_activity)) %>% 
  mutate(type_activity_factor = ifelse(type_activity == "leg_activity" & country_id == "Ireland", "Speeches (1000 words)",
                                       ifelse(type_activity == "vote_activity", "Roll-call votes (%)", 
                                              ifelse(type_activity == "speeches", "Speeches", "Procedural activity")))) %>% 
  mutate(activity_unique = paste0(cycle_id,":\n",type_activity_factor))



## Figure 2 ----

ep_activity_procedural <- ggplot(filter(data_combined_ep_act, type_activity == "Monthly procedural activity"),
                                 aes(x = use_campaign, y = leg_activity)) +
  geom_jitter(alpha = 0.4, size = 3) + 
  geom_smooth(se = FALSE, size = 1) +
  facet_wrap(~cycle_id, scales = "free_x") + 
  labs(x = "Campaign activity", 
       y = "Monthly procedural legislative effort")


ep_activity_votes <- ggplot(filter(data_combined_ep_act, type_activity == "Participation in roll-call votes"),
                            aes(x = use_campaign, y = leg_activity)) +
  geom_jitter(alpha = 0.4, size = 3) + 
  geom_smooth(se = FALSE, size = 1) +
  facet_wrap(~cycle_id, scales = "free_x") + 
  labs(y = "Participation in roll-call votes (%)", x = "Campaign activity")


grid_summary_ep <- cowplot::plot_grid(ep_activity_procedural, 
                                      ep_activity_votes, 
                                      align = "v", nrow = 2, rel_heights = c(1/2, 1/2))

grid_summary_ep

ggsave(plot = grid_summary_ep, 
       "fig_02.pdf", 
       width = 10, height = 7)
ggsave(plot = grid_summary_ep, 
       "fig_02.png", 
       width = 10, height = 7)


ep_6_reg <- ep_6_reg %>% 
  mutate(party_group = car::recode(party_group, recode_partygroup_ep))

ep_6_reg$party_group[ep_6_reg$party_group=="Group of the European People's Party (Christian Democrats) and European Democrats"] <- "EPP"

ep_7_reg$party_group[ep_7_reg$party_group=="Group of the European People's Party (Christian Democrats) and European Democrats"] <- "EPP"

glm_nb_ep <- MASS::glm.nb(leg_activity ~
                            use_campaign_scaled01  * cycle_id +  
                            female + poly(age, 2) + open_list + committee_chair + 
                            incumbent  * female +
                            partypos + 
                            country + party_group,
                          data = filter(data_combined_ep, 
                                        !is.na(age)))


data_ep_6 <- data_combined_ep %>% 
  filter(cycle_id == "European Parliament 6 (2004-2009)" & !is.na(age))

glm_nb_ep_6 <- MASS::glm.nb(leg_activity ~
                              use_campaign +
                              poly(age, 2) + female * incumbent +  open_list + 
                              committee_chair   + partypos +  party_group + 
                              country,
                            data = data_ep_6)

data_ep_7 <- data_combined_ep %>% 
  filter(cycle_id == "European Parliament 7 (2009-2014)" & !is.na(age))

glm_nb_ep_7 <- MASS::glm.nb(leg_activity ~
                              use_campaign +
                              poly(age, 2) + female * incumbent + open_list + 
                              committee_chair + incumbent + party_group + 
                              country,
                            data = data_ep_7)

lm_ep_vote <- lm(vote_activity ~
                   use_campaign_scaled01  * cycle_id +  
                   poly(age, 2) + female * incumbent + open_list + committee_chair + 
                   incumbent  +
                   partypos + 
                   country + party_group,
                 data = filter(data_combined_ep, 
                               !is.na(age)))

lm_ep_vote_6 <- lm(vote_activity ~
                     use_campaign +  
                     poly(age, 2) + female * incumbent + 
                     open_list + committee_chair + 
                     incumbent  +
                     partypos + 
                     country + party_group,
                   data = data_ep_6)

lm_ep_vote_7 <- lm(vote_activity ~
                     use_campaign + 
                     poly(age, 2) + female * incumbent + 
                     open_list + committee_chair + 
                     incumbent  +
                     partypos + 
                     country + party_group,
                   data = data_ep_7)


## Table 1 ----

coef_names_ep <- c("(Intercept)", 
                   "Campaign activity (stand.)",
                   "EP 7 (2009-2014)",
                   "Female", 
                   "Age",
                   "Age squared",
                   "Open List",
                   "Committee chair",
                   "Incumbent",
                   "Party office",
                   "Party: ECR",
                   "Party: EFD",
                   "Party: EPP",
                   "Party: EUL-NGL",
                   "Party: Greens-EFA",
                   "Party: IND/DEM",
                   "Party: Other",
                   "Party: SD",
                   "Party: UEN",
                   "Campaign activity (stand.) * EP 7 (2009-2014)",
                   "Female * Incumbent",
                   "Campaign activity (absolute)")


htmlreg(list(glm_nb_ep,
             lm_ep_vote,
             glm_nb_ep_6,
             lm_ep_vote_6,
             glm_nb_ep_7,
             lm_ep_vote_7),
        omit.coef = 'country*',
        single.row = TRUE,
        custom.coef.names = coef_names_ep,
        custom.model.names = c("M1: Procedural (stand.)",
                               "M2: Roll-call (stand.)",
                               "M3: Procedural: EP 6",
                               "M4: Formal: EP 6",
                               "M5: Procedural: EP 7",
                               "M6: Formal: EP 7"),
        file = "tab_01.htm",
        caption = "")


eff_ep_6 <- Effect("use_campaign",
                   glm_nb_ep_6, xlevels = 30) %>% 
  as.data.frame()


eff_ep_7 <- Effect("use_campaign",
                   glm_nb_ep_7, xlevels = 30) %>% 
  as.data.frame()

eff_ep_6_votes <- Effect("use_campaign",
                         lm_ep_vote_6, xlevels = 30) %>% 
  as.data.frame()

eff_ep_7_votes <- Effect("use_campaign",
                         lm_ep_vote_7, xlevels = 30) %>% 
  as.data.frame()

plot_eff_ep_6 <- ggplot(eff_ep_6, 
                        aes(x = use_campaign, y = fit)) +
  geom_line() + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2) +
  labs(x = "Campaign activity", y = "Predicted count of legislative activity",
       title = "European Parliament 6 (2004-2009)") +
  theme(plot.title = element_text(hjust = 0.5))

plot_eff_ep_6_votes <- ggplot(eff_ep_6_votes, 
                              aes(x = use_campaign, y = fit)) +
  geom_line() + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2) +
  labs(x = "Campaign activity", y = "Predicted participation in roll-call votes  (%)",
       title = "European Parliament 6 (2004-2009)") +
  theme(plot.title = element_text(hjust = 0.5))


plot_eff_ep_7 <- ggplot(eff_ep_7, 
                        aes(x = use_campaign, y = fit)) +
  geom_line() + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2) +
  labs(x = "Campaign activity", y = "Predicted count of legislative activity",
       title = "European Parliament 7 (2009-2014)") +
  theme(plot.title = element_text(hjust = 0.5))

plot_eff_ep_7_votes <- ggplot(eff_ep_7_votes, 
                              aes(x = use_campaign, y = fit)) +
  geom_line() + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2) +
  labs(x = "Campaign activity", y = "Predicted participation in roll-call votes (%)",
       title = "European Parliament 7 (2009-2014)") +
  theme(plot.title = element_text(hjust = 0.5))


plot_hist_ep_6 <- ggplot(data = filter(ep_6_reg, !is.na(age) & !is.na(use_campaign) & !is.na(leg_activity)), 
                         aes(x = use_campaign)) +
  geom_bar(alpha = 0.4) + 
  labs(x = "Campaign activity", y = "Count") +
  theme_void()

plot_hist_ep_7 <- ggplot(data = filter(ep_7_reg, !is.na(age) & !is.na(use_campaign) & !is.na(leg_activity)), 
                         aes(x = use_campaign)) +
  geom_bar(alpha = 0.4) + 
  labs(x = "Campaign activity", y = "Count") +
  theme_void()

plot_hist_ep_6_votes <- ggplot(data = filter(ep_6_reg, !is.na(age) & !is.na(use_campaign) & !is.na(vote_activity)), 
                               aes(x = use_campaign)) +
  geom_bar(alpha = 0.4) + 
  labs(x = "Campaign activity", y = "Count") +
  theme_void()

plot_hist_ep_7_votes <- ggplot(data = filter(ep_7_reg, !is.na(age) & !is.na(use_campaign) & !is.na(vote_activity)), 
                               aes(x = use_campaign)) +
  geom_bar(alpha = 0.4) + 
  labs(x = "Campaign activity", y = "Count") +
  theme_void()



grid_ep_6 <- cowplot::plot_grid(plot_eff_ep_6, 
                                plot_hist_ep_6, 
                                align = "v", nrow = 2,
                                rel_heights = c(4/5, 1/5))

grid_ep_7 <- cowplot::plot_grid(plot_eff_ep_7, 
                                plot_hist_ep_7, 
                                align = "v", nrow = 2,
                                rel_heights = c(4/5, 1/5))

grid_ep_6_votes <- cowplot::plot_grid(plot_eff_ep_6_votes, 
                                      plot_hist_ep_6_votes, 
                                      align = "v", nrow = 2,
                                      rel_heights = c(4/5, 1/5))

grid_ep_7_votes <- cowplot::plot_grid(plot_eff_ep_7_votes, 
                                      plot_hist_ep_7_votes, 
                                      align = "v", nrow = 2,
                                      rel_heights = c(4/5, 1/5))

grid_ep_effects <- cowplot::plot_grid(grid_ep_6, 
                                      grid_ep_7,
                                      align = "v", nrow = 1)
grid_ep_effects


## Figure 3 ----

ggsave(grid_ep_effects, file = "fig_03.pdf",
       width = 13, height = 5)
ggsave(grid_ep_effects, file = "fig_03.png",
       width = 13, height = 5)


## Figure 4 ----

grid_ep_effects_votes <- cowplot::plot_grid(grid_ep_6_votes, 
                                            grid_ep_7_votes,
                                            align = "v", nrow = 1)
ggsave(grid_ep_effects_votes, file = "fig_04.pdf",
       width = 13, height = 5)
ggsave(grid_ep_effects_votes, file = "fig_04.png",
       width = 13, height = 5)



data_combined_national <- data_combined %>% 
  filter(cycle_id %in% c("Germany (2005-2009)",
                         "Ireland (2007-2011)",
                         "Ireland (2011-2016)"))



## Figure 5 ----

ggplot(data_combined_ire_ger, aes(x = use_campaign, 
                                  y = leg_activity,
                                  colour = government,
                                  shape = government)) +
  geom_jitter(alpha = 0.8, size = 2) +
  scale_colour_grey(name = NULL, start = 0.7, end = 0.3) +
  scale_shape_discrete(name = NULL) +
  geom_smooth(se = FALSE) +
  facet_wrap(~activity_unique, scales = "free", nrow = 2) +
  labs(x = "Campaign activity", y = "Legislative activity")
ggsave("fig_05.pdf", 
       width = 10, height = 8)
ggsave("fig_05.png", 
       width = 10, height = 8)



dail_30_reg <- dail_30_reg %>% 
  mutate(government = as.factor(government))

dail_30_reg$government <- factor(dail_30_reg$government,
                                 levels = c("Opposition", "Government"))


dail_31_reg <- dail_31_reg %>% 
  mutate(government = as.factor(government))


dail_31_reg$government <- factor(dail_31_reg$government,
                                 levels = c("Opposition", "Government"))

glm_nb_dail_30 <- MASS::glm.nb(round(words_total_1000, 0) ~
                                 use_campaign  * government +
                                 female * incumbent +  poly(age, 2) +
                                 committee_chair +
                                 minister +
                                 government,
                               data = filter(dail_30_reg, 
                                             !is.na(age)))


glm_nb_dail_31 <- MASS::glm.nb(round(words_total_1000, 0) ~
                                 use_campaign * government +
                                 female * incumbent + poly(age, 2) +
                                 committee_chair +
                                 minister +
                                 government,
                               data = filter(dail_31_reg, 
                                             !is.na(age)))


lm_dail_31_vote <- lm(vote_activity ~
                        use_campaign * government +
                        female * incumbent  + poly(age, 2) +
                        committee_chair +
                        minister +
                        government,
                      data = filter(dail_31_reg, 
                                    !is.na(age)))

bt_16_reg <- bt_16_reg %>% 
  mutate(government = as.factor(government))


bt_16_reg$government <- factor(bt_16_reg$government,
                               levels = c("Opposition", "Government"))

glm_nb_bt_16 <- MASS::glm.nb(leg_activity ~
                               use_campaign * government +
                               female * incumbent +  poly(age, 2) +
                               committee_chair +
                               government + list_candidate,
                             data = filter(bt_16_reg, 
                                           !is.na(age)))

glm_nb_bt_16_speeches <- MASS::glm.nb(speeches ~
                                        use_campaign * government +
                                        female * incumbent + poly(age, 2) +
                                        committee_chair +
                                        government + list_candidate,
                                      data = filter(bt_16_reg, 
                                                    !is.na(age)))


## Table 2 ----

coef_names_nat <- c("(Intercept)",
                    "Campaign activity",
                    "Government",
                    "Female",
                    "Age",
                    "Age squared",
                    "Incumbent",
                    "Committee chair",
                    "Minister",
                    "Campaign activity * Opposition", 
                    "Female * Incumbent", 
                    "List candidate")

screenreg(list(glm_nb_dail_30, glm_nb_dail_31, glm_nb_bt_16))

htmlreg(list(glm_nb_dail_30, glm_nb_dail_31, glm_nb_bt_16),
        custom.coef.names = coef_names_nat,
        single.row = TRUE,
        custom.model.names = c("M1: Ireland 30 (words)", 
                               "M2: Ireland 31 (words)",
                               "M3: Germany 16 (procedural)"),
        caption = "",
        file = "tab_02.htm")

eff_bt_16 <- Effect(c("use_campaign", "government"),
                    glm_nb_bt_16, xlevels = 30) %>% 
  as.data.frame()

plot_eff_bt_16 <- ggplot(eff_bt_16, 
                         aes(x = use_campaign, y = fit,
                             fill = government)) +
  scale_fill_grey(start = 0.3, end = 0.7, name = NULL) +
  geom_line() + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.7) +
  labs(x = "Campaign activity", y = "Predicted count of legislative activity",
       title = "Germany (2005-2009)") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.justification = c(0, 0), 
        legend.position = c(0.3, 0.7))


eff_bt_16_speeches <- Effect(c("use_campaign", "government"),
                             glm_nb_bt_16_speeches, xlevels = 30) %>% 
  as.data.frame()

plot_eff_bt_16_speeches <- ggplot(eff_bt_16_speeches, 
                                  aes(x = use_campaign, y = fit, fill = government)) +
  scale_fill_grey(start = 0.3, end = 0.7, name = NULL) +
  geom_line() + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.7) +
  labs(x = "Campaign activity", y = "Predicted number of speeches",       
       title = "Germany (2005-2009)") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.justification = c(0, 0), 
        legend.position = c(0.4, 0.76))


eff_dail_31_vote <- Effect(c("use_campaign", "government"),
                           lm_dail_31_vote, xlevels = 30) %>% 
  as.data.frame() 


eff_dail_31 <- Effect(c("use_campaign", "government"),
                      glm_nb_dail_31, xlevels = 30) %>% 
  as.data.frame() 

eff_dail_30 <- Effect(c("use_campaign", "government"),
                      glm_nb_dail_30, xlevels = 30) %>% 
  as.data.frame() 

plot_eff_dail_30 <- ggplot(eff_dail_30, 
                           aes(x = use_campaign, y = fit, fill = government)) +
  scale_fill_grey(start = 0.3, end = 0.7, name = NULL) +
  geom_line() + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.7) +
  labs(x = "Campaign activity", y = "Predicted number of words (in 1000)",       
       title = "Ireland (2007-2011)") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.justification = c(0, 0), 
        legend.position = c(0.3, 0.7))

plot_eff_dail_31 <- ggplot(eff_dail_31, 
                           aes(x = use_campaign, y = fit,
                               fill = government)) +
  scale_fill_grey(start = 0.3, end = 0.7, name = NULL) +
  geom_line() + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.6) +
  labs(x = "Campaign activity", y = "Predicted number of words (in 1000)",
       title = "Ireland (2011-2016)") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.justification = c(0, 0), 
        legend.position = c(0.3, 0.7))

plot_eff_dail_31

plot_eff_dail_31_vote <- ggplot(eff_dail_31_vote, 
                                aes(x = use_campaign, y = fit,
                                    fill = government)) +
  scale_fill_grey(start = 0.3, end = 0.7, name = NULL) +
  geom_line() + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.6) +
  labs(x = "Campaign activity",
       y = "Predicted participation in roll-call votes",
       title = "Ireland (2011-2016)") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.justification = c(0, 0), 
        legend.position = c(0.4, 0.76))

plot_hist_dail_30 <- ggplot(data = filter(dail_30_reg, 
                                          !is.na(age) & 
                                            !is.na(use_campaign) & !is.na(words_total_1000)), 
                            aes(x = use_campaign)) +
  geom_bar(alpha = 0.4) + 
  labs(x = "Campaign activity", y = "Count") +
  theme(panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        strip.background = element_blank(),
        strip.text.x = element_blank()) +
  theme_void()


plot_hist_dail_31 <- ggplot(data = filter(dail_31_reg, !is.na(age) & !is.na(use_campaign) & !is.na(words_total_1000)), 
                            aes(x = use_campaign)) +
  geom_bar(alpha = 0.4) + 
  labs(x = "Campaign activity", y = "Count") +
  theme(panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        strip.background = element_blank(),
        strip.text.x = element_blank()) +  
  theme_void()

plot_hist_dail_31_vote <- ggplot(data = filter(dail_31_reg, !is.na(age) & !is.na(use_campaign) & !is.na(vote_activity)), 
                                 aes(x = use_campaign)) +
  geom_bar(alpha = 0.4) + 
  labs(x = "Campaign activity", y = "Count") +
  theme(panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        strip.background = element_blank(),
        strip.text.x = element_blank()) +
  theme_void()

plot_hist_bt_16 <- ggplot(data = filter(bt_16_reg, !is.na(age) & !is.na(use_campaign) & !is.na(leg_activity)), 
                          aes(x = use_campaign)) +
  geom_bar(alpha = 0.4) + 
  labs(x = "Campaign activity", y = "Count") +
  theme(panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        strip.background = element_blank(),
        strip.text.x = element_blank()) +
  theme_void()

plot_hist_bt_16_speeches <- ggplot(data = filter(bt_16_reg, !is.na(age) & !is.na(use_campaign) & !is.na(speeches)), 
                                   aes(x = use_campaign)) +
  geom_bar(alpha = 0.4) + 
  labs(x = "Campaign activity", y = "Count") +
  theme(panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        strip.background = element_blank(),
        strip.text.x = element_blank()) +
  theme_void()

grid_dail_30 <- cowplot::plot_grid(plot_eff_dail_30, 
                                   plot_hist_dail_30, 
                                   align = "v", nrow = 2,
                                   rel_heights = c(4/5, 1/5))


grid_dail_31 <- cowplot::plot_grid(plot_eff_dail_31, 
                                   plot_hist_dail_31, 
                                   align = "v", nrow = 2,
                                   rel_heights = c(4/5, 1/5))


grid_dail_31_vote <- cowplot::plot_grid(plot_eff_dail_31_vote, 
                                        plot_hist_dail_31_vote, 
                                        align = "v", nrow = 2,
                                        rel_heights = c(4/5, 1/5))

grid_bt_16 <- cowplot::plot_grid(plot_eff_bt_16, 
                                 plot_hist_bt_16, 
                                 align = "v", nrow = 2,
                                 rel_heights = c(4/5, 1/5))

grid_bt_16_speeches <- cowplot::plot_grid(plot_eff_bt_16_speeches, 
                                          plot_hist_bt_16, 
                                          align = "v", nrow = 2,
                                          rel_heights = c(4/5, 1/5))


grid_nat_effects <- cowplot::plot_grid(grid_dail_30, 
                                       grid_dail_31, 
                                       grid_bt_16,
                                       align = "v", nrow = 1)
grid_nat_effects
ggsave(grid_nat_effects, file = "fig_06.pdf",
       width = 13, height = 5)
ggsave(grid_nat_effects, file = "fig_06.png",
       width = 13, height = 5)


### Appendix -----

y <- data.frame()

dta$gender <- factor(dta$gender, levels = c("Male", "Female"))

for (i in unique(dta$country_id)) {
  glm_country <- glm(elected_dummy ~ use_campaign + incumbent + 
                       gender,
                     family = binomial(link = "logit"),
                     data = filter(dta, country_id == i))
  print(cat("country", i))
  
  print(screenreg(glm_country))
  
  eff_df <- Effect(c("use_campaign"), 
                   glm_country, xlevels = 25) %>% 
    as.data.frame() %>% 
    mutate(country_id = i)
  
  y <- rbind(y, eff_df)
}

obs_by_country <- dta %>% 
  group_by(country_id, elected_dummy) %>% 
  summarise(n_class = n()) %>% 
  group_by(country_id) %>% 
  mutate(n_total = sum(n_class)) %>% 
  filter(elected_dummy ==1) %>% 
  mutate(n_obs = paste0("n=", n_total)) %>% 
  mutate(prop_elected = paste0(round(n_class/n_total * 100, 1), "% elected"))


## Figure A 1 ----

ggplot(y, aes(x = use_campaign, y = fit)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2) +
  geom_line() + 
  labs(x = "Campaign activity", 
       y = "Predicted probabilitiy of winning a seat") + 
  facet_wrap(~country_id, scales = "free_x", nrow = 5) +
  geom_text(data = obs_by_country, aes(label = n_obs,
                                       x = 2, y = 0.95), size = 3, hjust = 0) +
  geom_text(data = obs_by_country, aes(label = prop_elected,
                                       x = 2, y = 0.75), size = 3, hjust = 0) +
  theme(strip.text = element_text(size = 12),
        axis.title = element_text(size = 10),
        axis.text = element_text(size = 9))
ggsave("fig_a01.pdf", 
       width = 8, height = 10)
ggsave("fig_a01.png", 
       width = 8, height = 10)



ttest_activity <- data.frame()
for (i in unique(dail_30_long$var)) {
  x_subset <- filter(dail_30_long, var == i & !is.na(value))
  ttest_store <- t.test(x_subset$leg_activity ~
                          as.factor(x_subset$value)) %>% 
    broom::tidy() %>% 
    mutate(activity = i) %>% 
    mutate(cycle_id = unique(x_subset$cycle_id))
  
  ttest_activity <- bind_rows(ttest_activity, ttest_store)
}


dail_31_long_subset <- dail_31_long %>%
  group_by(var) %>%
  mutate(n_yes = sum(value, na.rm = TRUE)) %>%
  filter(n_yes < 65)

for (i in unique(dail_31_long_subset$var)) {
  x_subset <- filter(dail_31_long_subset, var == i & !is.na(value))
  ttest_store <- t.test(x_subset$leg_activity ~
                          as.factor(x_subset$value)) %>% 
    broom::tidy() %>% 
    mutate(activity = i) %>% 
    mutate(cycle_id = unique(x_subset$cycle_id))
  
  ttest_activity <- bind_rows(ttest_activity, ttest_store)
}


ep_6_long_subset <- ep_6_long %>% 
  group_by(var) %>% 
  mutate(n_yes = sum(value, na.rm = TRUE)) %>% 
  filter(n_yes < 200)

for (i in unique(ep_6_long_subset$var)) {
  x_subset <- filter(ep_6_long_subset, var == i & !is.na(value))
  ttest_store <- t.test(x_subset$leg_activity ~
                          as.factor(x_subset$value)) %>% 
    broom::tidy() %>% 
    mutate(activity = i) %>% 
    mutate(cycle_id = unique(x_subset$cycle_id))
  
  ttest_activity <- bind_rows(ttest_activity, ttest_store)
}

ep_7_long_subset <- ep_7_long %>% 
  group_by(var) %>% 
  mutate(n_yes = sum(value, na.rm = TRUE)) %>% 
  filter(n_yes < 180)


for (i in unique(ep_7_long_subset$var)) {
  x_subset <- filter(ep_7_long_subset, var == i & !is.na(value))
  ttest_store <- t.test(x_subset$leg_activity ~
                          as.factor(x_subset$value)) %>% 
    broom::tidy() %>% 
    mutate(activity = i) %>% 
    mutate(cycle_id = unique(x_subset$cycle_id))
  
  ttest_activity <- bind_rows(ttest_activity, ttest_store)
}


bt_16_long_subset <- bt_16_long %>% 
  group_by(var) %>% 
  mutate(sum_value = sum(as.numeric(value), na.rm = TRUE)) %>% 
  filter(sum_value > 6) %>% 
  filter(sum_value < 210)


for (i in unique(bt_16_long_subset$var)) {
  x_subset <- filter(bt_16_long_subset, var == i)
  ttest_store <- t.test(x_subset$leg_activity ~
                          as.factor(x_subset$value)) %>%
    broom::tidy() %>%
    mutate(activity = i) %>%
    mutate(cycle_id = unique(x_subset$cycle_id))
  
  ttest_activity <- bind_rows(ttest_activity, ttest_store)
}

ttest_activity_plot <- ttest_activity %>% 
  mutate(significant = ifelse(conf.high > 0 & conf.low > 0, 
                              "Statistically significant",
                              ifelse(conf.high < 0 & conf.low < 0, 
                                     "Statistically significant",
                                     "Statistically insignificant")))

cat(paste(nrow(ttest_activity_plot), "separate t-tests"))

length(unique(dail_31_long$var)) +
  length(unique(dail_30_long$var)) +
  length(unique(ep_6_long$var)) +
  length(unique(ep_7_long$var)) +
  length(unique(bt_16_long$var)) 


prop.table(table(ttest_activity_plot$significant))

ttest_activity_plot <- ttest_activity_plot %>% 
  mutate(activity_unique = paste(activity, cycle_id))


## Figure A 2 ----

grid_nat_effects_app <- cowplot::plot_grid(grid_dail_31_vote, 
                                           grid_bt_16_speeches,
                                           align = "v", nrow = 1)
ggsave(grid_nat_effects_app, file = "fig_a02.pdf",
       width = 13, height = 5.5)


## Figure A 3 ----

ggplot(ttest_activity_plot, aes(x = reorder(activity_unique, estimate), y = estimate)) +
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high)) + 
  geom_hline(yintercept = 0, linetype = "dashed", colour = "red") +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) +
  annotate("text", x = 60, y = 0.6, 
           label = "Higher legislative activity", size = 5) +
  annotate("text", x = 20, y = -0.4, 
           label = "Lower legislative activity", size = 5) +
  labs(y = "Difference in Means (and 95%CI)",
       x = "Campaign activity")
ggsave("fig_a03.pdf", 
       width = 10, height = 5)
ggsave("fig_a03.png", 
       width = 10, height = 5)

ttest_activity_plot_sig <- ttest_activity_plot %>% 
  filter(significant == "Statistically significant")

ttest_activity_plot_sig <- ttest_activity_plot_sig %>% 
  mutate(activity  = str_replace_all(activity, "use_", ""))

table(ttest_activity_plot_sig$activity)        

recode_activity <- c("'campaignvideo'='Campaign video';
                     'fundraisingevents'='Fundraising events';
                     'internetdiscussions'='Online discussions';
                     'letters'='Letters'; 'socialmedia'='Social media';
                     'localtvradio'='Local TV/radio'; 
                     'discussions'='Discussions';
                     'natnewspaper'='National newspaper';
                     'nattvradio'='National TV/radio';
                     'socialgatherings'='Social gatherings';
                     'newsletters'='Newsletter';
                     'townhallmeetings'='Town hall meetings';
                     'officehours'='Office hours';
                     'spots'='TV spot';
                     'stickers'='Stickers'")

ttest_activity_plot_sig <- ttest_activity_plot_sig %>% 
  mutate(activity_factor = car::recode(activity, recode_activity))

ttest_activity_plot_sig <- ttest_activity_plot_sig %>% 
  mutate(cycle_id_short = car::recode(cycle_id, 
                                      "'European Parliament 6 (2004-2009)'='EP 6';
                                      'European Parliament 7 (2009-2014)'='EP 7';
                                      'Ireland (2007-2011)'='Ireland (07-11)';
                                      'Ireland (2011-2016)'='Ireland (11-16)';
                                      'Germany (2005-2009)'='Germany (05-09)'"))

ttest_activity_plot_sig$cycle_id_short <- factor(ttest_activity_plot_sig$cycle_id_short,
                                                 levels = c("EP 6", "EP 7",
                                                            "Ireland (07-11)", "Ireland (11-16)",
                                                            "Germany (05-09)"))

## Figure A 4 ----

ggplot(ttest_activity_plot_sig, 
       aes(x = reorder(activity_factor, estimate), y = estimate)) +
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high)) + 
  scale_shape_discrete(name = NULL) +
  scale_y_continuous(limits = c(-0.5, 0.5), breaks = c(seq(-0.5, 0.5, 0.1))) +
  geom_hline(yintercept = 0, linetype = "dashed", colour = "red")  +
  coord_flip() +
  annotate("text", x = 0.7, y = 0.4, label = "Higher legislative activity",
           colour = "grey50") +
  annotate("text", x = 0.7, y = -0.4, label = "Lower legislative activity",
           colour = "grey50") +
  facet_grid(cycle_id_short~., scales = "free", space = "free") +
  labs(y = "Difference in Means (and 95%CI)",
       x = NULL)
ggsave("fig_a04.pdf", 
       width = 10, height = 10)
ggsave("fig_a04.png", 
       width = 10, height = 10)


ccs_combined_ordinal$gender <- factor(ccs_combined_ordinal$gender,
                                      levels = c("Male", "Female"))


length(unique(ccs_combined_ordinal$country_id))

length(unique(ccs_combined_ordinal$study_id))

table(ccs_combined_ordinal$study_id)

glmer_ordinal <- glmer(elected_dummy ~ use_campaign * incumbent + 
                         gender +
                         (1 | study_id),
                       family = binomial(link = "logit"),
                       data = ccs_combined_ordinal)


## Table A 1 ----

coef_glmer <- c("(Intercept)", "Campaign activity", "Incumbent",
                "Female", "Campaign activity * Incumbent")

screenreg(list(glmer_dummies, glmer_ordinal))

texreg::htmlreg(list(glmer_dummies, glmer_ordinal),
                custom.model.names = 
                  c("M1: Additive binary index", 
                    "M2: Additive ordinal index"),
                custom.coef.names = coef_glmer,
                caption = "",
                custom.gof.names = c("AIC", "BIC", "Log Likelihood",
                                     "N", "N (Surveys)", "Var (Study ID)"),
                file = "tab_a01.htm")

## Table A 2 (created manually) ----

data_combined_ep_country_means <- data_combined_ep %>% 
  group_by(cycle_id, country) %>% 
  mutate(use_campaign_scaled01 = use_campaign_scaled01 - mean(use_campaign_scaled01, na.rm = TRUE)) 

glm_nb_ep_country_means <- MASS::glm.nb(leg_activity ~
                                          use_campaign_scaled01  * cycle_id +  
                                          female + poly(age, 2) + open_list + committee_chair + 
                                          incumbent  +
                                          partypos + 
                                          country + party_group,
                                        data = filter(data_combined_ep_country_means, 
                                                      !is.na(age)))

coef_names_ep_country_means <- c("(Intercept)", 
                                 "Campaign activity (stand.)",
                                 "EP 7 (2009-2014)",
                                 "Female", 
                                 "Age",
                                 "Age squared",
                                 "Open List",
                                 "Committee chair",
                                 "Incumbent",
                                 "Party office",
                                 "Party: ECR",
                                 "Party: EFD",
                                 "Party: EPP",
                                 "Party: EUL-NGL",
                                 "Party: Greens-EFA",
                                 "Party: IND/DEM",
                                 "Party: Other",
                                 "Party: SD",
                                 "Party: UEN",
                                 "Campaign activity (stand.) * EP 7 (2009-2014)",
                                 "Female x Incumbent")

screenreg(list(glm_nb_ep,
               glm_nb_ep_country_means),
          omit.coef='country*')

## Table A 3 ----
htmlreg(list(lm_dail_31_vote, glm_nb_bt_16_speeches),
        custom.coef.names = coef_names_nat,
        single.row = TRUE,
        custom.model.names = c("M1: Ireland 31 (vote)",
                               "M2: Germany 16 (speeches)"),
        caption = "",
        file = "tab_a03.htm")


## Table A 4 ----
htmlreg(list(glm_nb_ep,
             glm_nb_ep_country_means),
        omit.coef = 'country*',
        single.row = TRUE,
        custom.coef.names = coef_names_ep_country_means,
        custom.model.names = c("M1: Procedural",
                               "M2: Procedural (country means)"),
        file = "tab_a04.htm",
        caption = "")


glm_nb_ep_6_ord <- MASS::glm.nb(leg_activity ~
                                  use_ord_total  + poly(age, 2) + 
                                  female * incumbent + 
                                  open_list + 
                                  committee_chair + 
                                  partypos +  party_group + 
                                  country,
                                data = ep_6_reg)

ep_7_reg <- ep_7_reg %>% 
  mutate(party_group = as.factor(car::recode(party_group, recode_partygroup_ep))) 

glm_nb_ep_7_ord <- MASS::glm.nb(leg_activity ~
                                  use_ord_total +
                                  female * incumbent +  poly(age, 2) + open_list + 
                                  committee_chair + party_group + 
                                  country,
                                data = ep_7_reg)

glm_nb_bt_16_ord <- MASS::glm.nb(leg_activity ~
                                   use_ord_total * government +
                                   female * incumbent + poly(age, 2) +
                                   committee_chair +
                                   government + list_candidate,
                                 data = filter(bt_16_reg, 
                                               !is.na(age)))

glm_nb_dail_30_ord <- MASS::glm.nb(round(words_total_1000, 0) ~
                                     use_ord_total  * government +
                                     female * incumbent + poly(age, 2) +
                                     committee_chair +
                                     minister +
                                     government,
                                   data = filter(dail_30_reg, 
                                                 !is.na(age)))

glm_nb_dail_31_ord <- MASS::glm.nb(round(words_total_1000, 0) ~
                                     use_ord_total  * government +
                                     female * incumbent + poly(age, 2) +
                                     committee_chair +
                                     minister +
                                     government,
                                   data = filter(dail_31_reg, 
                                                 !is.na(age)))


coef_names_ord <- c("(Intercept)", 
                    "Campaign activity (ordinal)",
                    "Age",
                    "Age squared",
                    "Open List",
                    "Committee chair",
                    "Incumbent",
                    "Female",
                    "Party office",
                    "Party: EPP",
                    "Party: EUL-NGL",
                    "Party: Greens-EFA",
                    "Party: IND/DEM",
                    "Party: Other",
                    "Party: SD",
                    "Party: UEN",
                    "Female * Incumbent",
                    "Party: ECR",
                    "Party: EFD",
                    "Government",
                    "Minister",
                    "Campaign activity * Government",
                    "List candidate")

screenreg(list(glm_nb_ep_6_ord, 
               glm_nb_ep_7_ord,
               glm_nb_dail_30_ord,
               glm_nb_dail_31_ord,
               glm_nb_bt_16_ord), omit.coef = "country*")

## Table A 5 ----
htmlreg(list(glm_nb_ep_6_ord, 
             glm_nb_ep_7_ord,
             glm_nb_dail_30_ord,
             glm_nb_dail_31_ord,
             glm_nb_bt_16_ord),
        omit.coef = "country*",
        single.row = TRUE,
        custom.coef.names = coef_names_ord,
        caption = "",
        custom.model.names = c("M1: EP6",
                               "M2: EP7",
                               "M3: Dail 30",
                               "M4: Dail 31",
                               "M5: Bundestag 16"),
        file = "tab_a05.htm")

## Table A 6 (created manually) ----


#--------------------------------------------------------------------------
# Script for creating figures
#--------------------------------------------------------------------------


# European Parliament 6 (2004-2009)
EP6 <- read.csv("EP6_final.csv", sep = ",", header = TRUE, fileEncoding = "latin1")

# European Parliament 7 (2009-2014)
EP7 <- read.csv("EP7_final.csv", sep = ",", header = TRUE, fileEncoding = "utf-8")

# Dail 30 (2007-2011)
Dail30 <- read.csv("Dail30_final.csv", sep = ",", header = TRUE, fileEncoding = "utf-8") %>% 
  mutate(words_total_1000 = words_total/1000)


# Dail 31 (2011-2016)
Dail31 <- read.csv("Dail31_final.csv", sep = ",", header = TRUE, fileEncoding = "utf-8") %>% 
  mutate(words_total_1000 = words_total/1000) 


# Bundestag 16 (2005-2009)
BT16 <- read.csv("BT16_final.csv", sep = ",", header = TRUE, fileEncoding = "utf-8")

options(scipen=10000)

# Create figure with distribution of campaign index scores ----------------

# Create plots which compare respondents and non-respondents -------------

# Note: An MP is coded as a respondent if s/he has answered more than 1 campaign question 
# and has been a valid legislative activity score.

# Find the mean of each group ---------------------------------------------

# Get mean values of both groups (respondents vs. non-respondents)

library(plyr) # load package only now -- otherwise problems with dplyr

# EP 6
EP6$Respondent <- factor(car::recode(EP6$use_campaign, "0=0; 1=0; else=1"))

EP6_filter <- filter(EP6, leg_activity_month < 50 & # one outlier included in dataset
                       age > 1)

meanEP6 <- ddply(EP6_filter, "Respondent", numcolwise(mean, na.rm = TRUE))


# EP 7
EP7$Respondent <- factor(car::recode(EP7$use_campaign, "0=0; 1=0; else=1"))

EP7_filter <- filter(EP7, #leg_activity_month > 1 & 
                     leg_activity_month < 70 &
                       age > 1)
meanEP7 <- ddply(EP7_filter, "Respondent", numcolwise(mean, na.rm = TRUE))


# Dail 30
Dail30$Respondent <- factor(car::recode(Dail30$use_campaign, "0=0; NA=0; else=1"))

Dail30_filter <- filter(Dail30, age > 1  & 
                          party != "Ceann Comhairle")
meanDail30 <- ddply(Dail30_filter, "Respondent", numcolwise(mean, na.rm = TRUE))

# Dail 31
Dail31$Respondent <- factor(car::recode(Dail31$use_campaign, "0=0; NA=0; 1=0; else=1"))

Dail31_filter <- filter(Dail31, speeches_total > 1 & 
                          age > 1)
meanDail31 <- ddply(Dail31_filter, "Respondent", numcolwise(mean, na.rm = TRUE))


# BT 16
BT16_filter <- filter(BT16, wonseat == 1 & 
                        leg_activity > 0)

BT16_filter$Respondent <- factor(car::recode(BT16_filter$use_campaign, "0=0; NA=0; 1=0; else=1"))

meanBT16 <- ddply(BT16_filter, "Respondent", numcolwise(mean, na.rm = TRUE))


# Write functions for plots

ggplot_comparison_density <- function(data_new, data_mean, x, xlab_new){
  ggplot(data_new, aes_q(x = substitute(as.numeric(x)), 
                         colour = substitute(as.factor(Respondent)))) +
    geom_vline(data= data_mean,
               aes_q(xintercept=substitute(as.numeric(x)),
                     colour=substitute(as.factor(Respondent))),
               linetype="dashed", size=.6) +
    geom_density() +
    scale_colour_grey(breaks = c(0, 1), start = 0.7, end = 0,
                      name = "Respondent",
                      labels = c("No", "Yes")) +
    theme(panel.grid.major = element_line(size = 0.4),
          legend.position = "bottom") +
    xlab(substitute(xlab_new)) +
    ggtitle(substitute(xlab_new)) +
    labs(y = "Density")
}




# Legislative activity


EP6_age <- ggplot_comparison_density(EP6_filter, meanEP6, age, "Age")

EP7_age <- ggplot_comparison_density(EP7_filter, meanEP7, age, "Age")

Dail30_age <- ggplot_comparison_density(Dail30_filter, meanDail30, age, "Age")

Dail31_age <- ggplot_comparison_density(Dail31_filter, meanDail31, age, "Age")

BT16_age <- ggplot_comparison_density(BT16_filter, meanBT16, age, "Age")


# Legislative activity


EP6_la <- ggplot_comparison_density(EP6_filter, meanEP6, leg_activity_month, 
                                    "Legislative activity")

EP7_la <- ggplot_comparison_density(EP7_filter, meanEP7, leg_activity_month, 
                                    "Legislative activity")

Dail30_la <- ggplot_comparison_density(Dail30_filter, meanDail30, words_total_1000, 
                                       "Words spoken in parliament (in 1000)")

Dail31_la <- ggplot_comparison_density(Dail31_filter, meanDail31, words_total, 
                                       "Words spoken in parliament (in 1000)")

BT16_la <- ggplot_comparison_density(BT16_filter, meanBT16, speeches, 
                                     "Number of speeches")



# Gender


EP6_gender <- ggplot(EP6_filter, aes(x = Respondent, fill = as.factor(female))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_grey(start = 0.7, end = 0.1,
                  name = NULL,
                  labels = c("Male", "Female")) + 
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Gender")

EP7_gender <- ggplot(EP7_filter, aes(x = Respondent, fill = as.factor(female))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_grey(start = 0.7, end = 0.1,
                  name = NULL,
                  labels = c("Male", "Female")) + 
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL,  title = "Gender")


Dail30_gender <- ggplot(Dail30_filter, aes(x = Respondent, fill = as.factor(female_elected))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_grey(start = 0.7, end = 0.1,
                  name = NULL,
                  labels = c("Male", "Female")) + 
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Gender")
Dail30_gender

Dail31_gender <- ggplot(Dail31_filter, aes(x = Respondent, fill = as.factor(female))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_grey(start = 0.7, end = 0.1,
                  name = NULL,
                  labels = c("Male", "Female")) + 
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Gender")
Dail31_gender

BT16_gender <- ggplot(BT16_filter, aes(x = Respondent, fill = as.factor(female_elected))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_grey(start = 0.7, end = 0.1,
                  name = NULL,
                  labels = c("Male", "Female")) + 
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL,  title = "Gender")
BT16_gender




# Open list (EPs only)


# change bulgaria
EP6_openlist <- ggplot(EP6_filter, aes(x = Respondent, fill = as.factor(open_list))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_grey(start = 0.7, end = 0.1,
                  name = NULL,
                  labels = c("Close list", "Open list")) + 
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Open vs. close list")
EP6_openlist

#   
# ggplot_comparison_barplot(EP6_filter, open_list, "Open list vs. close list") +
#   scale_fill_brewer(palette="Greys", name = substitute("Open List"), labels = c("No", "Yes"))

# change croatia
EP7_openlist <- ggplot(EP7_filter, aes(x = Respondent, fill = as.factor(open_list))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_grey(start = 0.7, end = 0.1,
                  name = NULL,
                  labels = c("Close list", "Open list")) + 
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Open vs. close list")



# Parties


# EP 6
EP6_filter <-  EP6_filter[order(EP6_filter$party_publish), ]

EP6_parties <- ggplot(EP6_filter, 
                      aes(x = Respondent, fill = as.factor(party_publish))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = NULL) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Party group")


# EP 7
EP7_filter <-  EP7_filter[order(EP7_filter$party_publish), ]

EP7_parties <- ggplot(EP7_filter, 
                      aes(x = Respondent, fill = as.factor(party_publish))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = NULL) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Party group")


# Dail 30 
Dail30_filter <-  Dail30_filter[order(Dail30_filter$party), ]

Dail30_parties <- ggplot(Dail30_filter, 
                         aes(x = Respondent, fill = as.factor(party))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = NULL) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Party group")


Dail31_filter <- Dail31_filter %>% 
  mutate(party_elected = car::recode(party_elected, 
                                     "'The Labour Party'='Labour';
                                     'Green Party'='Greens';
                                     'People Before Profit Alliance'='PBP';
                                     'Workers and Umeployed Action Group'='WUAG'"))
# Dail 31 
Dail31_filter <-  Dail31_filter[order(Dail31_filter$party_elected), ]


# change NA party
Dail31_parties <- ggplot(Dail31_filter, 
                         aes(x = Respondent, fill = as.factor(party_elected))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = NULL) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Party group")

Dail31_parties

# BT 16 
BT16_filter <-  BT16_filter[order(BT16_filter$fraktion), ]

# change NA party
BT16_parties <- ggplot(BT16_filter, 
                       aes(x = Respondent, fill = as.factor(party))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = NULL) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Party group")



# Incumbent


# Dail 30

# change NA party
Dail31_incumbent <- ggplot(Dail31_filter, 
                           aes(x = Respondent, fill = as.factor(incumbent))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Incumbent"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Incumbent")

Dail30_incumbent <- ggplot(Dail30_filter, 
                           aes(x = Respondent, fill = as.factor(incumbent))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Incumbent"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Incumbent")



# Campaign spending


# Dail 30

Dail30_filter_spending <- filter(Dail30_filter, 
                                 campaign_spending1000 != "NA")

meanDail30_filter_spending <- ddply(Dail30_filter_spending, "Respondent", numcolwise(mean))


Dail30_spending <- ggplot_comparison_density(Dail30_filter_spending, meanDail30_filter_spending, campaign_spending1000, 
                                             "Campaign spending (in 1000 euros)")

# Dail 31

meanDail31$campaign_spending <- car::recode(meanDail31$campaign_spending1000, 
                                            "NA=18870")

Dail31_filter_spending <- filter(Dail31_filter, 
                                 campaign_spending1000 != "NA")
meanDail31_filter_spending <- ddply(Dail31_filter_spending, "Respondent", numcolwise(mean))

Dail31_spending <- ggplot_comparison_density(Dail31_filter_spending, meanDail31_filter_spending, campaign_spending1000, 
                                             "Campaign spending (in 1000 euros)")


# Committee chair/vice chair



EP6_committee_chair <- ggplot(EP6_filter, 
                              aes(x = Respondent, fill = as.factor(committee_chair))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Committee\nchair/vice chair"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Committee chair/vice chair")


EP7_committee_chair <- ggplot(EP7_filter, 
                              aes(x = Respondent, fill = as.factor(committee_chair))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Committee\nchair/vice chair"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Committee chair/vice chair")


Dail30_committee_chair <- ggplot(Dail30_filter, 
                                 aes(x = Respondent, fill = as.factor(committee_chair))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Committee\nchair/vice chair"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Committee chair/vice chair")

Dail31_committee_chair <- ggplot(Dail31_filter, 
                                 aes(x = Respondent, fill = as.factor(committee_chair))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Committee\nchair/vice chair"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Committee chair/vice chair")


BT16_committee_chair <- ggplot(BT16_filter, 
                               aes(x = Respondent, fill = as.factor(committee_chair))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Committee\nchair/vice chair"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Committee chair/vice chair")



# Party group chair/vice chair


EP6_partypos <- ggplot(EP6_filter, 
                       aes(x = Respondent, fill = as.factor(partypos))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Party group\nchair/vice chair"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Party group chair/vice chair")

EP7_partypos <- ggplot(EP7_filter, 
                       aes(x = Respondent, fill = as.factor(partypos))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Party group\nchair/vice chair"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Party group chair/vice chair")



# Minister


Dail30_minister <- ggplot(Dail30_filter, 
                          aes(x = Respondent, fill = as.factor(minister))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Minister"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Minister")

Dail31_minister <- ggplot(Dail31_filter, 
                          aes(x = Respondent, fill = as.factor(minister))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Minister"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Minister")

BT16_minister <- ggplot(BT16_filter, 
                        aes(x = Respondent, fill = as.factor(minister))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Minister"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Minister")


# Opposition leader


# Dail 30
Dail30_opposition_leader <-  ggplot(Dail30_filter, 
                                    aes(x = Respondent, fill = as.factor(opposition_leader))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Opposition\nleader"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Opposition leader")


Dail31_opposition_leader <-  ggplot(Dail31_filter, 
                                    aes(x = Respondent, fill = as.factor(opposition_leader))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Opposition\nleader"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Opposition leader")



BT16_opposition_leader <-  ggplot(BT16_filter, 
                                  aes(x = Respondent, fill = as.factor(opposition_leader))) +
  geom_bar(position = "fill", colour = "black") +
  scale_fill_brewer(palette="Greys", name = substitute("Opposition\nleader"), labels = c("No", "Yes")) +
  scale_x_discrete(labels = c("Non-respondent", "Respondent")) +
  labs(y = "Proportion", x = NULL, title = "Opposition leader")


# Figure A 5 ----
arrangeEP6 <- grid.arrange(EP6_la,
                           EP6_age,
                           EP6_gender,
                           EP6_parties,
                           EP6_partypos,
                           EP6_committee_chair, 
                           EP6_openlist, ncol = 2)
ggsave("fig_a05.pdf", arrangeEP6, width = 11, height = 15)
ggsave("fig_a05.png", arrangeEP6, width = 11, height = 15)


# Figure A 6 ----
arrangeEP7 <- grid.arrange(EP7_la,
                           EP7_age,
                           EP7_gender,
                           EP7_parties,
                           EP7_partypos,
                           EP7_committee_chair, 
                           EP7_openlist, ncol = 2)
ggsave("fig_a06.pdf", arrangeEP7, width = 11, height = 15)
ggsave("fig_a06.png", arrangeEP7, width = 11, height = 15)

## Figure A 7 ----
arrangeDail30 <- grid.arrange(Dail30_la,
                              Dail30_spending,
                              Dail30_age,
                              Dail30_gender,
                              Dail30_incumbent,
                              Dail30_committee_chair,
                              Dail30_opposition_leader, 
                              Dail30_minister, 
                              Dail30_parties, ncol = 2)

ggsave("fig_a07.pdf", arrangeDail30, width = 11, height = 15)
ggsave("fig_a07.png", arrangeDail30, width = 11, height = 15)


### Figure A 8 ----
arrangeDail31 <- grid.arrange(Dail31_la,
                              Dail31_spending,
                              Dail31_age,
                              Dail31_gender,
                              Dail31_incumbent,
                              Dail31_committee_chair,
                              Dail31_opposition_leader, 
                              Dail31_minister, 
                              Dail31_parties, ncol = 2)

ggsave("fig_a08.pdf", 
       arrangeDail31, width = 11, height = 15)
ggsave("fig_a08.png", 
       arrangeDail31, width = 11, height = 15)


## Figure A 9 ----
arrangeBT16 <- grid.arrange(BT16_la,
                            BT16_age,
                            BT16_gender,
                            BT16_committee_chair,
                            BT16_opposition_leader, 
                            BT16_minister, 
                            BT16_parties, ncol = 2)
ggsave("fig_a09.pdf", arrangeBT16, width = 11, height = 15)
ggsave("fig_a09.png", arrangeBT16, width = 11, height = 15)

